package net.sudot.chess.business.wechat.message;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import net.sudot.chess.business.model.Customer;
import net.sudot.chess.business.model.OrderGame;
import net.sudot.chess.business.model.User;
import net.sudot.chess.config.SystemProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 牌局结束提醒
 * <pre>
 * {{first.DATA}}
 * 牌局名称：{{keyword1.DATA}}
 * 参与盘数：{{keyword2.DATA}}
 * 游戏结果：{{keyword3.DATA}}
 * {{remark.DATA}}
 * <p/>
 * 最终发送示例:
 * <p/>
 * xxx，您参与的牌局已结束
 *
 * 牌局名称：xxx
 * 参与盘数：第xx期
 * 游戏结果：${订单状态}
 * </pre>
 *
 * @author tangjialin on 2018-04-01.
 */
@Component
public class EndGameTemplateMessage extends AbstractTemplateMessage {

    @Value("${system.config.wechat.endGameTemplateId}")
    private String templateId;

    @Override
    public String getTemplateId() {
        return templateId;
    }

    @Override
    protected WxMpTemplateMessage.WxMpTemplateMessageBuilder builderTemplateMessage(User user, Object data, SystemProperties systemProperties) {
        Customer customer = (Customer) user;
        OrderGame orderGame = (OrderGame) data;
        String gameTitle = orderGame.getMemo();
        List<WxMpTemplateData> templateData = new ArrayList<>();
        templateData.add(new WxMpTemplateData("first", customer.getNickname() + "，您参与的牌局已结束\n"));
        templateData.add(new WxMpTemplateData("keyword1", gameTitle));
        templateData.add(new WxMpTemplateData("keyword2", gameTitle.substring(1, gameTitle.indexOf("]"))));
        templateData.add(new WxMpTemplateData("keyword3", orderGame.getStatus().memo()));
        templateData.add(new WxMpTemplateData("remark", ""));
        WxMpTemplateMessage.WxMpTemplateMessageBuilder builder = WxMpTemplateMessage.builder().data(templateData);
//        builder.url(systemProperties.getWebsite() + "/chat/index");
        return builder;
    }
}
